package org.ufinity.hsms.module;

import org.ufinity.hsms.R;
import org.ufinity.hsms.module.alarm.AlarmStackActivity;
import org.ufinity.hsms.module.busy.BusyStackActivity;
import org.ufinity.hsms.module.device.DeviceStackActivity;
import org.ufinity.hsms.module.home.HomeStackActivity;
import org.ufinity.hsms.module.panel.PanelStackActivity;
import org.ufinity.hsms.module.setting.SettingStackActivity;

import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

public class MainActivity extends ActivityGroup implements OnTabChangeListener {

	Context instance;

	private TabHost mainTabHost;
	protected TabWidget mTabWidget;
	protected int currentTabID = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		instance = this;
		int tabId = getIntent().getIntExtra("tabId", 0);
		//currentTabID = tabId;
		init();
	}


	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		int tabId = getIntent().getIntExtra("tabId", 0);
		
		currentTabID = tabId;
		mainTabHost.setCurrentTab(currentTabID);
		currentTabID = 0;
		onTabChanged(String.valueOf(tabId));
		
	/*	mainTabHost.setCurrentTab(currentTabID);
		onTabChanged(String.valueOf(currentTabID));*/
	}
	
	

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		int tabId = intent.getIntExtra("tabId", 0);
		//onTabChanged(intent.getStringExtra("tabId"));
		mainTabHost.setCurrentTab(currentTabID);
	}

	protected void init() {
		// TODO Auto-generated method stub
		setContentView(R.layout.main_view);
		initTabs();
	}

	/**
	 * @Title: initTabs
	 * @param
	 * @return void
	 * @throws
	 */
	protected void initTabs() {

		mainTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
		mainTabHost.setOnTabChangedListener(this);
		mainTabHost.setup(this.getLocalActivityManager());
		initIntent();
	}

	protected void initIntent() {
		setIndicator(0, new Intent(this, HomeStackActivity.class),
				R.string.home,R.drawable.home_icon);
		setIndicator(1, new Intent(this, PanelStackActivity.class),
				R.string.master_control_panel,R.drawable.dashboard_icon);
		setIndicator(2, new Intent(this, AlarmStackActivity.class),
				R.string.alarm,R.drawable.alarm_icon);
		setIndicator(3, new Intent(this, DeviceStackActivity.class),
				R.string.monitor_data,R.drawable.monitordata_icon);
		setIndicator(4, new Intent(this, BusyStackActivity.class),
				R.string.bussy_monitor,R.drawable.bussymonitor_icon);
		setIndicator(5, new Intent(this, SettingStackActivity.class),
				R.string.setting,R.drawable.setting_icon);

		
	}

	protected void setIndicator(int tabId, Intent intent,int tabName, int icon) {

		View localView = LayoutInflater.from(this.mainTabHost.getContext())
				.inflate(R.layout.main_tab_widget_view, null);
		String str = String.valueOf(tabId);
		TextView textview=(TextView) localView.findViewById(R.id.main_activity_tab_text);
		textview.setText(getResources().getString(tabName));
		
		((ImageView) localView.findViewById(R.id.main_activity_tab_image))
		.setBackgroundResource(icon);
		
		TabHost.TabSpec localTabSpec = mainTabHost.newTabSpec(str)
				.setIndicator(localView).setContent(intent);
		if (mainTabHost != null) {
			mainTabHost.addTab(localTabSpec);
		}
	}

	@Override
	public void onTabChanged(String tabId) {
		int tabID = Integer.valueOf(tabId);
		currentTabID=tabID;
		for (int i = 0; i < mTabWidget.getTabCount(); i++) {
			mTabWidget.getChildAt(Integer.valueOf(i)).setBackgroundResource(
					R.drawable.bg_footer01);
		}
		// 改变选中背景颜色
		mTabWidget.getChildAt(Integer.valueOf(tabID)).setBackgroundResource(
				R.drawable.bg_footer02);
	}
}

